const Service = require('egg').Service;

class AddrService extends Service {
  async getAll() {
    const { app } = this;
    return await app.mysql.select('address')
  }
  async add(obj) {
    const { app } = this;

    return await app.mysql.insert('address', obj)
  }
  async edit(obj) {
    const { app } = this;
    return await app.mysql.update('address', obj)
  }
  async del(params) {
    const { app } = this;
    return await app.mysql.delete('address', params)
  }

  async setDefault({ id }) {
    const { app } = this;
    console.log(id, '这是传过来的 id--------------------')
    // 第一步先将数据库中的所有 默认设为 0 
    //     update address set is_default=0 where is_default=1 and id=15;
    this.app.mysql.query(`update address set is_default=${0} where is_default=${1}`);
    return await app.mysql.query(`update address set is_default=${1} where is_default=${0} and id=${id}`)
  }
}

module.exports = AddrService;